//Validator 的意思是 验证器
const { getUerInfo } = require('../service/user.service') 

const userValidator = async (ctx,next) =>{
    const {user_name,password } = JSON.parse(ctx.request.body)
     //合法性
     if(!user_name || !password){
        console.error('用户名或密码为空',ctx.request.body);//暴露具体错误信息
        ctx.status = 400 //显示状态
        ctx.body = {
            code:'10001',
            message:"用户名或密码为空",
            result:''
        }
        return
    }
    await next()
}

//合理性
const verifyUser = async (ctx,next) => {
    const {user_name}  = ctx.request.body
    console.log(getUerInfo({user_name}));
    if( getUerInfo({user_name})){
    // console.log(getUerInfo({user_name}));
        ctx.status = 409  //表示冲突
        ctx.body = {
            code:"10002",
            message:'用户已经存在',
            result:''
        }
        return
    }
    await next()
}

module.exports={
    userValidator,verifyUser
}